package com.hanvon.face;

import android.app.Activity;
import android.content.Context;
import android.graphics.Canvas;
import android.graphics.Color;
import android.graphics.Paint;
import android.graphics.PixelFormat;
import android.graphics.PorterDuff.Mode;
import android.hardware.Camera;
import android.hardware.Camera.CameraInfo;
import android.util.AttributeSet;
import android.util.Log;
import android.view.Surface;
import android.view.SurfaceHolder;
import android.view.SurfaceView;
import com.hanvon.faceRec.*;
import net.sunzc.hwfacetest.utils.MyLog;

import java.io.IOException;
import java.util.Arrays;
import java.util.List;

public class HanvonfaceAddUserView extends SurfaceView implements SurfaceHolder.Callback, Camera.PreviewCallback {

    private static int PIXEL_WIDTH = 640;// 1280,800,768,720,640,576,480,384,352
    private static int PIXEL_HEIGHT = 480;// 720,480,432,480,480,432,320,288,288

    private String TAG = "RegisterCameraPreview";
    private Camera mCamera = null;//Camera
    private SurfaceHolder mSurfaceHolder, mSurfaceHolder02; // mSurfaceHolder 相机Preview，mSurfaceHolder02 显示框
    private SurfaceView mSurfaceView02;//mSurfaceHolder02 显示框 对应SurfaceView
    private boolean bPreviewing = false;
    private int mOpenedCamera = 0;        //前置摄像机或后置摄像机
    private Context mContext;

    public boolean bTest = false;
    private OUserInfo userInfo = null;
    private int userImageNum = 0;
    private int TEMPLATE_SIZE = 5;
    private byte[] rotateData;
    private IRegisterCallback mRegisterCallback = null; //回调函数

    byte[] pFrFaceFeature;
    byte[] tempbyte;
    private int mRotation = 0;    //翻转角度
    private String mStrUserName = "";
    private int mUserID = 0;
    private boolean mbInited = false;

    private int[] mpnBigFacePos = new int[12 * 10];
    private int m_nFeatureSize = 1024;
    private byte[] m_pbFeatures = null;
    //
    private int[] g_LastEye = new int[6];
    private byte[] g_abLastTpltFtr = null;
    private int mnBigMaxFace = 1;
    private int[] mpnDetectBigFaceNum = new int[1];
    private boolean mbRegisterDetect = false;
    private int mnCurrentRegisterNum = 1;
    private int mFrmCount = 0;

    private int DIST_SCALE = 10 * 10; //10分之一的眼距。
    private int CHANGE_MIN = 3 * 3; //眼睛定位最小变化 3个像素
    private int CHANGE_MAX = 20 * 20;

    private int DIST_TH1 = 600;//第一距离比较需要超过阈值。
    private int MAX_TRY_TIME = 8;
    int g_iTryTime = 0;

    public HanvonfaceAddUserView(Context context, AttributeSet attrs) {
        super(context, attrs);
        // TODO Auto-generated constructor stub
        mContext = context;
    }

    public HanvonfaceAddUserView(Context context) {
        super(context);
        // TODO Auto-generated constructor stub
        mContext = context;
    }

    @Override
    public void surfaceChanged(SurfaceHolder arg0, int arg1, int arg2, int arg3) {
        // TODO Auto-generated method stub
        RestartCamera();
    }

    /**
     * 设置显示和绘制SurfaceView
     *
     * @param view02 绘制 Surface
     */
    public void setSurfaceView(SurfaceView view02) {
        /* 以SurfaceView作为相机Preview之用mSurfaceView01显示相机mSurfaceView02显示脸框  */
        mSurfaceView02 = view02;
        mSurfaceView02.setZOrderOnTop(true);
        //getSurfaceHolder();
    }

    /**
     * 初始化SDK
     *
     * @param nImgWidth     图像宽度
     * @param nImgHeihgt    图像高度
     * @param nOpenedCamera 前置或后置摄像机
     */
    public void onInitSDK(int nImgWidth, int nImgHeihgt, int nOpenedCamera, int nUserID, String sUserName) {

        PIXEL_WIDTH = nImgWidth;
        PIXEL_HEIGHT = nImgHeihgt;
        mOpenedCamera = nOpenedCamera;
        mStrUserName = sUserName;
        //mUserID = nUserID;

        int result05 = -1;
        /***********************************************************/
        /***人脸检测接口描述
         /***初始化函数
         /***********************************************************/
        Log.i(TAG, "scanCoachQRCode--InitFaceEngine");
        result05 = HWFaceLib.getInstance().InitFaceEngine(FaceCoreHelper.SERIAL_NUMBER);
        Log.i(TAG, "end--InitFaceEngine return:" + Integer.toString(result05));

        mbInited = true;
    }

    /**
     * 开启人脸检测
     */
    public void onStartPreview() {
        if (!mbInited) {
            onInitSDK(PIXEL_WIDTH, PIXEL_HEIGHT, mOpenedCamera, mUserID, mStrUserName);
        }

        try {
            initCamera();
        } catch (IOException e) {
            // TODO Auto-generated catch block
            e.printStackTrace();
        }

        //	mCamera.startPreview();
    }

    /**
     * 释放SDK资源
     */
    public void onReleaseSDK() {
        resetCamera();
        HWFaceLib.getInstance().ReleaseFaceEngine();
        //userInfo.pbImageArray = null;
        mbInited = false;
    }

    public void startRegister() {
        bTest = true;
    }

    /* func:停止preview,释放Camera对象 */
    private void resetCamera() {
        if (mCamera != null && bPreviewing) {
            mSurfaceHolder.removeCallback(this);
            mCamera.setPreviewCallback(null);
            mCamera.stopPreview();
            /* 释放Camera对象 */
            mCamera.release();
            mCamera = null;
            bPreviewing = false;
        }

    }

    /* func:停止preview */
    private void stopPreview() {
        if (mCamera != null && bPreviewing) {
            Log.v(TAG, "stopPreview");
            mCamera.stopPreview();
        }
    }

    /* get a fully initialized SurfaceHolder */
    private void getSurfaceHolder() {
        mSurfaceHolder = getHolder();
        mSurfaceHolder.addCallback(this);
        mSurfaceHolder.setType(SurfaceHolder.SURFACE_TYPE_PUSH_BUFFERS);
        mSurfaceHolder02 = mSurfaceView02.getHolder();
        mSurfaceHolder02.setFormat(PixelFormat.TRANSLUCENT);
    }

    private void setCameraPreviewOrientation(Activity activity,
                                             int cameraId, android.hardware.Camera camera) {
        android.hardware.Camera.CameraInfo info =
                new android.hardware.Camera.CameraInfo();
        android.hardware.Camera.getCameraInfo(cameraId, info);
        int rotation = activity.getWindowManager().getDefaultDisplay()
                .getRotation();
        int degrees = 0;
        switch (rotation) {
            case Surface.ROTATION_0:
                degrees = 0;
                break;
            case Surface.ROTATION_90:
                degrees = 90;
                break;
            case Surface.ROTATION_180:
                degrees = 180;
                break;
            case Surface.ROTATION_270:
                degrees = 270;
                break;
        }

        int result;
        if (info.facing == Camera.CameraInfo.CAMERA_FACING_FRONT) {
            result = (info.orientation + degrees) % 360;
            result = (360 - result) % 360;  // compensate the mirror
        } else {  // back-facing
            result = (info.orientation - degrees + 360) % 360;
        }
        camera.setDisplayOrientation(result);
        // mRotation = info.orientation;
        mRotation = 0;
    }

    private void initCamera() throws IOException {
        getSurfaceHolder();

        mSurfaceHolder02.setFormat(PixelFormat.TRANSLUCENT);
        Log.i(TAG, "scanCoachQRCode-initCamera return:" + Boolean.toString(bPreviewing));

        if (!bPreviewing) {
            mCamera = Camera.open(mOpenedCamera);
        }
        // 非预览时and相机打开时，开启preview
        if (mCamera != null && !bPreviewing) {

            Log.i(TAG, "inside the camera");
            /* 创建Camera.Parameters对象 */
            Camera.Parameters parameters = mCamera.getParameters();

			/* 设置相片格式为JPEG */
            parameters.setPictureFormat(PixelFormat.JPEG);
            if (PIXEL_WIDTH != 0 && PIXEL_HEIGHT != 0) {
                /* 指定preview的屏幕大小 */
                parameters.setPreviewSize(PIXEL_WIDTH, PIXEL_HEIGHT);
                /* 设置图片分辨率大小 */
                parameters.setPictureSize(PIXEL_WIDTH, PIXEL_HEIGHT);
            }

            parameters.setPreviewFrameRate(15);
            parameters.setJpegQuality(95);

            List<String> list = parameters.getSupportedAntibanding();
            if (list != null) {
                for (int i = 0; i < list.size(); i++) {
                    if (list.get(i).equals(Camera.Parameters.ANTIBANDING_50HZ)) {
                        parameters.setAntibanding(Camera.Parameters.ANTIBANDING_50HZ);
                    }
                }
            }
            list = parameters.getSupportedWhiteBalance();
            if (list != null) {
                for (int i = 0; i < list.size(); i++) {
                    if (list.get(i).equals(Camera.Parameters.WHITE_BALANCE_AUTO)) {
                        parameters.setWhiteBalance(Camera.Parameters.WHITE_BALANCE_AUTO);
                    }
                }
            }

            //parameters.setAntibanding(Camera.Parameters.ANTIBANDING_50HZ);
            //parameters.setColorEffect(Camera.Parameters.EFFECT_NONE);
            //parameters.setExposureCompensation(6);
            //parameters.setWhiteBalance(Camera.Parameters.WHITE_BALANCE_AUTO);

            //parameters.setSceneMode(Camera.Parameters.SCENE_MODE_SNOW);
            //parameters.setFlashMode(Camera.Parameters.FLASH_MODE_OFF);

            //parameters.setColorEffect(Camera.Parameters.EFFECT_BLACKBOARD);
            /* 将Camera.Parameters设置予Camera */
            mCamera.setParameters(parameters);

            try {
                setCameraPreviewOrientation((Activity) mContext, mOpenedCamera, mCamera); //FIXME: remove hardcode '0' here
                /* setPreviewDisplay唯一的参数为SurfaceHolder */
                mCamera.setPreviewDisplay(mSurfaceHolder);
            } catch (IOException e) {
                e.printStackTrace();
            }
            parameters = mCamera.getParameters();
            PIXEL_WIDTH = parameters.getPreviewSize().width;
            PIXEL_HEIGHT = parameters.getPreviewSize().height;

            int[] FrFaceFeatureLength = new int[1];
            HWFaceLib.getInstance().GetFeatureSize(FrFaceFeatureLength);
            m_nFeatureSize = FrFaceFeatureLength[0];

            g_abLastTpltFtr = new byte[m_nFeatureSize];

            userInfo = new OUserInfo();
            switch (mRotation) {
                case 0:
                    userInfo.nHeight = PIXEL_HEIGHT;
                    userInfo.nWidth = PIXEL_WIDTH;
                    break;
                default:
                    userInfo.nHeight = PIXEL_WIDTH;
                    userInfo.nWidth = PIXEL_HEIGHT;
                    break;
            }

            userInfo.strName = mStrUserName;
            userInfo.nImgNum = TEMPLATE_SIZE;
            Log.i(TAG, "scanCoachQRCode--showUserInfo.pbImageArray");

            userInfo.pbImageArray = new byte[PIXEL_WIDTH * PIXEL_HEIGHT * 2 * TEMPLATE_SIZE];

            Log.i(TAG, "end--showUserInfo.pbImageArray");
            userInfo.pFacePos = new int[12 * 16];
            m_pbFeatures = new byte[m_nFeatureSize * TEMPLATE_SIZE];

            rotateData = new byte[PIXEL_WIDTH * PIXEL_HEIGHT * 2];

/*			if(mUserID > 0)
            {
				HWFaceLib.getInstance().GetUserInfobyID(mUserID, userInfo);
			}*/
        }
    }

    public void RestartCamera() {
        if (bPreviewing && mCamera != null) {
            mCamera.stopPreview();
        } else {
            if (mCamera == null) {
                try {
                    initCamera();
                } catch (IOException e) {
                    // TODO Auto-generated catch block
                    e.printStackTrace();
                }
                mCamera.startPreview();
                bPreviewing = true;

            } else {
                Camera.Parameters params = mCamera.getParameters();
                if (PIXEL_WIDTH != 0 && PIXEL_HEIGHT != 0) {
                    params.setPreviewSize(PIXEL_WIDTH, PIXEL_HEIGHT);
                    params.setPictureSize(PIXEL_WIDTH, PIXEL_HEIGHT);
                }

                mCamera.setParameters(params);
                Log.i(TAG, "surfaceChanged");

                try {
                    setCameraPreviewOrientation((Activity) mContext, mOpenedCamera, mCamera); //FIXME: remove hardcode '0' here
                    mCamera.setPreviewDisplay(mSurfaceHolder);
                } catch (IOException e) {
                    e.printStackTrace();
                }
                mCamera.setPreviewCallback(this);
                mCamera.startPreview();
                bPreviewing = true;
            }
        }

    }

    @Override
    public void surfaceCreated(SurfaceHolder arg0) {
        // TODO Auto-generated method stub

    }

    @Override
    public void surfaceDestroyed(SurfaceHolder arg0) {
        // TODO Auto-generated method stub
        if (mCamera != null) {
            mCamera.setPreviewCallback(null);
            mCamera.stopPreview();
            mCamera.release();
        }
        mCamera = null;
        bPreviewing = false;
    }

    /**
     * 绘制函数
     *
     * @param canvas
     * @param facepos
     */
    void canvasDrawLine(Canvas canvas, FacePos facepos) {
        try {
            int startX, startY, stopX, stopY, Leftx, Lefty, Rightx, Righty;
            int heightf = mSurfaceHolder02.getSurfaceFrame().height();
            int widthf = mSurfaceHolder02.getSurfaceFrame().width();
            float hratio = (float) heightf / (float) PIXEL_HEIGHT;
            float wratio = (float) widthf / (float) PIXEL_WIDTH;

            switch (mRotation) {
                case 0:
                    break;
                default:
                    hratio = (float) heightf / (float) PIXEL_WIDTH;
                    wratio = (float) widthf / (float) PIXEL_HEIGHT;
                    break;
            }

            //通过outputParams获取坐标
            startX = facepos.nCol - ((facepos.nWidth / 2));
            startY = facepos.nRow - ((facepos.nHeight / 2));
            stopX = facepos.nCol + ((facepos.nWidth / 2));
            stopY = facepos.nRow + ((facepos.nHeight / 2));


            Paint mPaint = new Paint();
            mPaint.setColor(Color.GREEN);// 画笔为绿色
            mPaint.setStrokeWidth(2);// 设置画笔粗细
            mPaint.setTextSize(50.0f);

            //换算双眼连接中点
            if (startX == 0 && startY == 0 && stopX == 0 && stopY == 0) {

            } else {
                //添加 比例换算
                startX = (int) ((float) startX * wratio);
                startY = (int) ((float) startY * hratio);
                stopX = (int) ((float) stopX * wratio);
                stopY = (int) ((float) stopY * hratio);

                //canvas.drawLine(startX, startY, stopX, startY, mPaint);
                canvas.drawLine(startX, startY, startX + 30, startY, mPaint);
                canvas.drawLine(stopX - 30, startY, stopX, startY, mPaint);

                //canvas.drawLine(startX, startY, startX, stopY, mPaint);
                canvas.drawLine(startX, startY, startX, startY + 30, mPaint);
                canvas.drawLine(startX, stopY - 30, startX, stopY, mPaint);

                //canvas.drawLine(stopX, stopY, stopX, startY, mPaint);
                canvas.drawLine(stopX, stopY, stopX, stopY - 30, mPaint);
                canvas.drawLine(stopX, startY + 30, stopX, startY, mPaint);

                //canvas.drawLine(stopX, stopY, startX, stopY, mPaint);
                canvas.drawLine(stopX, stopY, stopX - 30, stopY, mPaint);
                canvas.drawLine(startX + 30, stopY, startX, stopY, mPaint);
            }


        } catch (Exception e) {
        }
    }

    private int GetDist(int x1, int y1, int x2, int y2) {
        return (x1 - x2) * (x1 - x2) + (y1 - y2) * (y1 - y2);
    }

    //private int AddTemplet( byte[] pbImg, int[] pCurEye,int iPos )
    private int AddTemplet(byte[] pbImg, int[] pCurEye, int[] pFacePos, int iPos) {
        int iResult = -1;
        int nDist;
        byte[] abFtr = new byte[this.m_nFeatureSize];
        int width = PIXEL_WIDTH;
        int height = PIXEL_HEIGHT;

        switch (mRotation) {
            case 0:
                break;
            default:
                width = PIXEL_HEIGHT;
                height = PIXEL_WIDTH;
                break;
        }


        //当前的眼距
        nDist = GetDist(pCurEye[0], pCurEye[1], pCurEye[2], pCurEye[3]);

        nDist /= DIST_SCALE;//10分之一的眼距。
        if (nDist < CHANGE_MIN)
            nDist = CHANGE_MIN;
        if (nDist > CHANGE_MAX)
            nDist = CHANGE_MAX;

        //眼睛定位要有变化
        if (GetDist(pCurEye[0], pCurEye[1], g_LastEye[0], g_LastEye[1]) > nDist ||
                GetDist(pCurEye[2], pCurEye[3], g_LastEye[2], g_LastEye[3]) > nDist) {
            //眼睛定位变化满足要求，
            int res = HWFaceLib.getInstance().GetFaceFeatureEx(pbImg, width, height, pCurEye, abFtr);
            //if( 0 == HWFaceLib.getInstance().GetFaceFeatureEx( pbImg, width, height, pCurEye, abFtr ))

            if (res == 0)

            {
                int[] iDist = new int[1];
                HWFaceLib.getInstance().CompareFeature(abFtr, g_abLastTpltFtr, iDist);//与上一个存储的模板比较.比较第一特征距离。
                if (iDist[0] > DIST_TH1 || g_iTryTime >= MAX_TRY_TIME)//不一样,或多次采集模板,防止总是相似就采集不下去了。
                {
                    System.arraycopy(abFtr, 0, m_pbFeatures, iPos * m_nFeatureSize, m_nFeatureSize);
                    System.arraycopy(g_abLastTpltFtr, 0, abFtr, 0, m_nFeatureSize);
                    System.arraycopy(pCurEye, 0, g_LastEye, 0, 4);

                    g_iTryTime = 0;

                    return 0;
                } else {
                    g_iTryTime++;
                }
            }
        }

        return iResult;
    }

    /**
     * 设置回调函数
     *
     * @param callback 回到函数
     */
    public void onRegisterResultCallback(IRegisterCallback callback) {
        this.mRegisterCallback = callback;
    }

    @Override
    public void onPreviewFrame(byte[] data, Camera arg1) {
        // TODO Auto-generated method stub
        int width = PIXEL_WIDTH;
        int height = PIXEL_HEIGHT;
      /*  byte[] rotateData = new byte[data.length];*/

        Canvas canvas = mSurfaceHolder02.lockCanvas(null);

        //后面长宽颠倒
        //TODO Auto-generated method stub
        if ((mFrmCount % 5) == 0) {
            //处理前后置摄像头
            switch (mRotation) {
                case 0:
                    width = PIXEL_WIDTH;
                    height = PIXEL_HEIGHT;
                    System.arraycopy(data, 0, rotateData, 0, data.length);
                    break;
                case 90:
                    width = PIXEL_HEIGHT;
                    height = PIXEL_WIDTH;
                    UtilFunc.rotateYuvData(rotateData, data, PIXEL_WIDTH, PIXEL_HEIGHT, 0);
                    break;
                case 270:
                    width = PIXEL_HEIGHT;
                    height = PIXEL_WIDTH;
                    UtilFunc.rotateYuvData(rotateData, data, PIXEL_WIDTH, PIXEL_HEIGHT, 1);
                    break;
            }


            if (bTest) {
                if (userInfo != null) {
                    canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);

                    //第一帧
                    if ((userImageNum == 0) && !mbRegisterDetect) {
                        MyLog.i(TAG, "当前宽度:" + width + "当前高度" + height);
                        int nRetOk = HWFaceLib.getInstance().FaceDetection(rotateData, width, height, mpnBigFacePos, mnBigMaxFace, mpnDetectBigFaceNum);
                        if ((nRetOk == 0) && (mpnDetectBigFaceNum[0] > 0)) {
                            if (mOpenedCamera == CameraInfo.CAMERA_FACING_FRONT) {
                                for (int k = 0; k < mpnDetectBigFaceNum[0]; k++) {
                                    mpnBigFacePos[k * 12 + 1] = width - mpnBigFacePos[k * 12 + 1];
                                    mpnBigFacePos[k * 12 + 5] = width - mpnBigFacePos[k * 12 + 5];
                                    mpnBigFacePos[k * 12 + 7] = width - mpnBigFacePos[k * 12 + 7];
                                }
                            }
                            FacePos facepos = HWFaceLib.getInstance().GetFacePos(mpnBigFacePos, 0);

                            canvasDrawLine(canvas, facepos);
                            mbRegisterDetect = true;
                        }
                    } else if ((userImageNum < TEMPLATE_SIZE) && mbRegisterDetect) {

                        int[] pnFacePos = new int[12];
                        int nMaxFace = 1;
                        int[] pnDetectFaceNum = new int[1];
                        int nRet = HWFaceLib.getInstance().FaceDetection(rotateData, width, height, pnFacePos, nMaxFace, pnDetectFaceNum);
                        Log.i(TAG, "facedetection:rotateData" + rotateData.length +
                                "width:" + width + "height:" + height + "pnDetectFaceNum:" + Arrays.toString(pnDetectFaceNum) + "facePos:" + Arrays.toString(pnFacePos));
                        if ((nRet == 0) && (pnDetectFaceNum[0] == 1)) {

                            int[] eyePos = new int[6];
                            System.arraycopy(pnFacePos, 5, eyePos, 0, 4);
                            int result = AddTemplet(rotateData, eyePos, pnFacePos, userImageNum);
                            if (result == 0) {

                                System.arraycopy(pnFacePos, 0, userInfo.pFacePos, userImageNum * 12, 12);
                                System.arraycopy(rotateData, 0, userInfo.pbImageArray, userImageNum * PIXEL_WIDTH * PIXEL_HEIGHT * 2, rotateData.length);
                                userImageNum++;
                                if (this.mRegisterCallback != null)
                                    this.mRegisterCallback.registerCallback(0, userImageNum, "录入第 " + String.valueOf(userImageNum) + " 张");

                            }
                            /**前置摄像头镜像处理，开始*/
                            if (mOpenedCamera == CameraInfo.CAMERA_FACING_FRONT) {
                                for (int k = 0; k < mpnDetectBigFaceNum[0]; k++) {
                                    pnFacePos[k * 12 + 1] = width - pnFacePos[k * 12 + 1];
                                    pnFacePos[k * 12 + 5] = width - pnFacePos[k * 12 + 5];
                                    pnFacePos[k * 12 + 7] = width - pnFacePos[k * 12 + 7];
                                }
                            }
                            /**前置摄像头镜像处理，结束*/
                            FacePos facepos = HWFaceLib.getInstance().GetFacePos(pnFacePos, 0);
                            canvasDrawLine(canvas, facepos);
                        } else if (pnDetectFaceNum[0] > 0) {
                            //if( this.mResultCallback != null )
                            //this.mResultCallback.resultCallback(1,null,null,null,"录入过程请保证只有一个人");
                        }
                    } else {
                        if (mbRegisterDetect && (userImageNum == TEMPLATE_SIZE)) {
                            //if( this.mResultCallback != null )
                            //this.mResultCallback.resultCallback(1,null,null,null,"模板已满");
                            int[] pnID = new int[1];
                            /*if(mUserID > 0)
                            {
								if(HWFaceLib.getInstance().UpdateUserInfobyID(mUserID, false, userInfo) == 0){
									if( this.mRegisterCallback != null )
										this.mRegisterCallback.registerCallback(1,0,"修改成功");
								}
							}
							else*/
                            {
                                HWFaceLib.getInstance().AddUserWithFeatures(userInfo, m_pbFeatures, TEMPLATE_SIZE, pnID);
                                //数据值信息处理反馈值
                                if (pnID[0] > -1) {
                                    int[] num = new int[1];
                                    HWFaceLib.getInstance().GetUserIDCount(num);
                                    Log.i(TAG, "HWFaceLib.AddUserWithFeatures,success,pnID:" + pnID[0] + "num:" + num[0]);

                                }

                                //发送识别结果
                                if (pnID[0] != -1) {
                                    //输出识别成功
                                    //int resultUinfo = HWFaceLib.getInstance().GetUserInfobyID(pnID[0], userInfo);
                                    //if(userInfo.nImgNum > 0 && resultUinfo ==0 )
                                    {
                                        /*
                                        FacePos pos = HWFaceLib.GetFacePos(userInfo.pFacePos, 0);
										OUserInfo info = new OUserInfo();
										info.pbImageArray = new byte[pos.nJpgLength];
										System.arraycopy(userInfo.pbImageArray ,0, info.pbImageArray, 0, pos.nJpgLength);
										info.strName = userInfo.strName;
										info.nWidth = userInfo.nWidth;
										info.nHeight = userInfo.nHeight;
										info.nImgNum = 1;
										*/
                                        if (this.mRegisterCallback != null)
                                            this.mRegisterCallback.registerCallback(1, 0, "创建成功");
                                    }

                                }
                            }


                            bTest = false;
                            mbRegisterDetect = false;
                            userImageNum = 0;
                        }

                    }
                } else {
                    canvas.drawColor(Color.TRANSPARENT, Mode.CLEAR);
                    bTest = false;
                    //if( this.mResultCallback != null )
                    //this.mResultCallback.resultCallback(1,null,null,null,"还未创建用户");
                }
            }
        }

        mFrmCount++;

        mSurfaceHolder02.unlockCanvasAndPost(canvas);
    }

}
